//
//  YJDetadetaViewController.m
//  健康饮食
//
//  Created by qianfeng01 on 15/7/6.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJDetadetaViewController.h"
#import "YJDetaDetaaModel.h"
#import "YJDetadetaCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "YJHeadDatamodel.h"
#import "YJMaterialDataModel.h"
#import "YJMaterialDataCell.h"
@interface YJDetadetaViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIImageView *_imageView;
    UILabel *_label;
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSMutableArray *_practiceArray;
    NSMutableArray *_materialArray;
    AFHTTPRequestOperationManager *_manager;
}

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *practiceArray;
@property (nonatomic,strong) NSMutableArray *materialArray;
@property (nonatomic,strong) AFHTTPRequestOperationManager *manage;
@end

@implementation YJDetadetaViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.dataArray =[[NSMutableArray alloc] init];
    self.materialArray =[[NSMutableArray alloc] init];
    self.practiceArray=[[NSMutableArray alloc] init];
    
    self.tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
   
    [self.tableView registerNib:[UINib nibWithNibName:@"YJMaterialDataCell" bundle:nil] forCellReuseIdentifier:@"YJMaterialDataCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"YJDetadetaCell" bundle:nil] forCellReuseIdentifier:@"YJDetadetaCell"];
    
    self.tableView.separatorStyle =NO;
    [self.view addSubview:self.tableView];
    
    [self creatHttpRequest];
    [self loadData];
    
}

-(void)loadData{
    AFHTTPRequestOperationManager *manage=[AFHTTPRequestOperationManager manager];
    manage.responseSerializer =[AFHTTPResponseSerializer serializer];
    
    NSString *url =[NSString stringWithFormat:kUrlDetadeta,self.code];
    [manage GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * detaDic =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dicts =detaDic[@"data"];
            YJHeadDatamodel *model =[[YJHeadDatamodel alloc] init];
            model.img =dicts[@"img"];
            model.name =dicts[@"name"];
            model.code =dicts[@"code"];
            NSArray *array =dicts[@"burden"];
            NSArray *imageArray =dicts[@"makes"];

            for (NSDictionary *dict in array) {
                YJMaterialDataModel *model =[[YJMaterialDataModel alloc] init];
                model.name = dict[@"name"];
                [self.materialArray addObject:model];
                
            }
            [self.dataArray addObject:self.materialArray];

            for (NSDictionary *dict in imageArray) {
                YJDetaDetaaModel *model =[[YJDetaDetaaModel alloc] init];
                model.num =dict[@"num"];
                model.info =dict[@"info"];
                model.image =dict[@"img"];
                
                [self.practiceArray addObject:model];
            }
            [self.dataArray addObject:self.practiceArray];
            [self addView:model];
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
- (void)addView:(YJHeadDatamodel *)model{
    
    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    _imageView =[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, self.view.frame.size.width-10, 300)];
    
    [_imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"food.8"]];
    
    _label=[[UILabel alloc] initWithFrame:CGRectMake(5, _imageView.frame.size.height-30, _imageView.frame.size.width, 30)];
    _label.backgroundColor =[UIColor grayColor];
    _label.text =model.name;
    
    [view addSubview:_imageView];
    [view addSubview:_label];
    self.tableView.tableHeaderView=view;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array =self.dataArray[section];
    
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
        YJMaterialDataCell *cell =[tableView dequeueReusableCellWithIdentifier:@"YJMaterialDataCell"];
        if (cell==nil) {
            cell =[[YJMaterialDataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YJMaterialDataCell"];
        }
        YJMaterialDataModel *model =self.materialArray[indexPath.row];
        [cell showDataWithModel:model];
        return cell;
    } else if(indexPath.section==1){
        
        YJDetadetaCell *cell =[tableView dequeueReusableCellWithIdentifier:@"YJDetadetaCell"];
        if (cell==nil) {
            cell =[[YJDetadetaCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"YJDetadetaCell"];
        }
        YJDetaDetaaModel *model =self.practiceArray[indexPath.row];
        [cell showWithModel:model];
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==1) {
        return 120;
    }else{
        return 40;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return @"用料";
    } else {
        return @"做法";
    }
}

@end
